package com.share.example.bcs.common.protocol.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class MsgBodyDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
        byte[] tmp1 = new byte[MsgBody.PROTOCOL_LEN];
        byte[] tmp2 = new byte[MsgBody.LENGTH_LEN];
        byteBuf.readBytes(tmp1);
        byteBuf.readBytes(tmp2);

        String protocol = new String(tmp1);//这个不需要
        String len = new String(tmp2);//数据长度
        int length = new Integer(len);
        if (length >= 0) {
            byte[] body = new byte[length];
            byteBuf.readBytes(body);
            out.add(body);
        }
    }
}
